// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/writer.h>
#include <pollux/exec/memory_reclaimer.h>
#include <pollux/exec/sort_buffer.h>

namespace kumo::pollux::dwio::common {
    /// Sorting Writer object is used to write sorted data into a single file.
    class SortingWriter : public Writer {
    public:
        SortingWriter(
            std::unique_ptr<Writer> writer,
            std::unique_ptr<exec::SortBuffer> sortBuffer,
            vector_size_t maxOutputRowsConfig,
            uint64_t maxOutputBytesConfig,
            uint64_t outputTimeSliceLimitMs);

        ~SortingWriter() override;

        void write(const VectorPtr &data) override;

        bool finish() override;

        /// No action because we need to accumulate all data and sort before data can
        /// be flushed.
        void flush() override;

        void close() override;

        void abort() override;

    private:
        class MemoryReclaimer : public exec::MemoryReclaimer {
        public:
            static std::unique_ptr<memory::MemoryReclaimer> create(
                SortingWriter *writer);

            bool reclaimableBytes(
                const memory::MemoryPool &pool,
                uint64_t &reclaimableBytes) const override;

            uint64_t reclaim(
                memory::MemoryPool *pool,
                uint64_t targetBytes,
                uint64_t maxWaitMs,
                memory::MemoryReclaimer::Stats &stats) override;

        private:
            MemoryReclaimer(SortingWriter *writer)
                : exec::MemoryReclaimer(0),
                  writer_(writer),
                  canReclaim_(writer_->sortBuffer_->canSpill()) {
            }

            SortingWriter *const writer_;
            const bool canReclaim_;
        };

        bool canReclaim() const;

        uint64_t reclaim(uint64_t targetBytes, memory::MemoryReclaimer::Stats &stats);

        vector_size_t outputBatchRows();

        const std::unique_ptr<Writer> outputWriter_;
        const vector_size_t maxOutputRowsConfig_;
        const uint64_t maxOutputBytesConfig_;
        const uint64_t finishTimeSliceLimitMs_;
        memory::MemoryPool *const sortPool_;
        const bool canReclaim_;

        std::unique_ptr<exec::SortBuffer> sortBuffer_;
    };
} // namespace kumo::pollux::dwio::common
